import {setToken,getToken} from '@/utils/auth'
import {login,getInfo} from '@/api/login'
const user = {
	state:{
		token: getToken(),
	},
	actions:{
		Login({ commit }, userInfo) {
			const username = userInfo.username.trim()
			const password = userInfo.password
			const code = userInfo.code
			const uuid = userInfo.uuid
			return new Promise((resolve,reject)=>{
				login(username,password,code,uuid).then(res=>{
					setToken(res.token)
					commit('SET_TOKEN', res.token)
					resolve()
				}).catch(error=>{
					reject(error)
				})
			})
		},
		//Promise 写法需要把写全部 不然返回获取不到，与流程中断
		GetInfo({commit},state){
			return new Promise((resolve,reject) =>{
				getInfo().then(res => {

					resolve(res)
				}).catch(error=>{
					reject(error)
				})
			})
		}
	},
	mutations:{
		SET_TOKEN: (state, token) => {
			state.token = token
		  },
	}
}
export default user